package com.tanxi521.afkreward.util;

import com.tanxi521.afkreward.AfkReward;
import com.tanxi521.afkreward.data.AFKRewardMapper;
import com.tanxi521.afkreward.pojo.PlayerAfk;
import me.clip.placeholderapi.PlaceholderAPI;
import me.clip.placeholderapi.PlaceholderHook;
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender;

import java.util.List;

public class RewardUtil {

    //换算时间
    public static String time(long time) {
        long res = time / 1000;
        long h = res / 3600;
        long m = (res - h * 3600) / 60;
        long s = (res - h * 3600) % 60;
        return "§e" + h + "§6时" + "§e" + m + "§6分" + "§e" + s + "§6秒";
    }

    //返回挂机持续时间字符串
    public static String afkTime(PlayerAfk playerAfk) {
        long start = playerAfk.getStart();
        long now = System.currentTimeMillis();
        boolean a = false;
        if (playerAfk.getEnd() != 0) {
            now = playerAfk.getEnd();
            a = true;
        }
        String res = time(now - start);
        if (a) {
            res = res + "§c(挂机掉线)";
        }
        return res;
    }

    //计算挂机结束后的奖励次数
    public static long rewardsTime(PlayerAfk playerAfk) {
        long start = playerAfk.getStart();
        long end = playerAfk.getEnd();
        long time = end - start;
        int rewardsTime = ConfigUtil.getRound();
        return time / 1000 / 60 / rewardsTime;
    }

    public static long giveRewards(PlayerAfk playerAfk, CommandSender sender) {
        Long rewardsTime = rewardsTime(playerAfk);
        Long maxTimes = playerAfk.getToday();
        int maxRewards = ConfigUtil.getMaxRest();
        Long lastTime = maxRewards - maxTimes;
        if (rewardsTime <= 0) {
            sender.sendMessage("§e[提示]本次挂机没有达到获得奖励时间哦");
            PlayerAfk pDO = new PlayerAfk();
            pDO.setPlayerName(playerAfk.getPlayerName());
            pDO.setPlayerUuid(playerAfk.getPlayerUuid());
            pDO.setStart(0L);
            pDO.setEnd(0L);
            pDO.setType("未挂机");
            pDO.setReward("未挂机");
            pDO.setTotal(playerAfk.getTotal() + (playerAfk.getEnd()) - playerAfk.getStart());
            pDO.setTimes(playerAfk.getTimes());
            pDO.setToday(playerAfk.getToday());
            AFKRewardMapper.updatePlayer(pDO);
            AFKRewardMapper.AFK_MAP.remove(playerAfk.getPlayerName());
            return 0;
        }

//        if (lastTime <= 0 && ConfigUtil.isMaxLimit()) {
//            sender.sendMessage("§e[提示]你今天已经无法通过挂机再获得更多奖励了哦, " + maxRewards + " 次");
//            PlayerAfk pDO = new PlayerAfk();
//            pDO.setPlayerName(playerAfk.getPlayerName());
//            pDO.setPlayerUuid(playerAfk.getPlayerUuid());
//            pDO.setStart(0L);
//            pDO.setEnd(0L);
//            pDO.setType("未挂机");
//            pDO.setReward("未挂机");
//            pDO.setTotal(playerAfk.getTotal());
//            pDO.setTimes(playerAfk.getTimes());
//            pDO.setToday(playerAfk.getToday());
//            AFKRewardMapper.updatePlayer(pDO);
//            AFKRewardMapper.AFK_MAP.remove(playerAfk.getPlayerName());
//            return 0;
//        }
        List<String> commands = ConfigUtil.getCommand(sender, playerAfk);
        if (rewardsTime >= lastTime && ConfigUtil.isMaxLimit()) {
            long realTime = sender.hasPermission("AfkRewards.not.reward.limit") ? rewardsTime : lastTime;
            long i = realTime;
            while (realTime > 0) {
                for (String cmd : commands) {
                    String command = CommandSplUtil.handle(cmd, sender).getCommand();
                    Bukkit.dispatchCommand(Bukkit.getConsoleSender(), command);
                }
                realTime--;
            }

            PlayerAfk pDO = new PlayerAfk();
            pDO.setPlayerName(playerAfk.getPlayerName());
            pDO.setPlayerUuid(playerAfk.getPlayerUuid());
            pDO.setStart(0L);
            pDO.setEnd(0L);
            pDO.setType("未挂机");
            pDO.setReward("未挂机");
            pDO.setTotal(playerAfk.getTotal() + (playerAfk.getEnd()) - playerAfk.getStart());
            pDO.setTimes(playerAfk.getTimes() + i);
            pDO.setToday((long) maxRewards);
            AFKRewardMapper.updatePlayer(pDO);
            AFKRewardMapper.AFK_MAP.remove(playerAfk.getPlayerName());
            if (!sender.hasPermission("AfkRewards.not.reward.limit"))
                sender.sendMessage("§e[提示]你今天已经到达每日奖励上限 " + maxRewards + " 次，无法继续获得挂机奖励");
            else sender.sendMessage("§e[提示]你今天已经到达每日奖励上限 " + maxRewards + " 次，但是你比较牛，无视了他");
            return i;
        }
        if (rewardsTime < lastTime || !ConfigUtil.isMaxLimit()) {
            long i = rewardsTime;
            while (rewardsTime > 0) {
                for (String cmd : commands) {
                    String command = CommandSplUtil.handle(cmd, sender).getCommand();
                    Bukkit.dispatchCommand(Bukkit.getConsoleSender(), command);
                }
                rewardsTime--;
            }
            PlayerAfk pDO = new PlayerAfk();
            pDO.setPlayerName(playerAfk.getPlayerName());
            pDO.setPlayerUuid(playerAfk.getPlayerUuid());
            pDO.setStart(0L);
            pDO.setEnd(0L);
            pDO.setType("未挂机");
            pDO.setReward("未挂机");
            pDO.setTotal(playerAfk.getTotal() + (playerAfk.getEnd()) - playerAfk.getStart());
            pDO.setTimes(playerAfk.getTimes() + i);
            pDO.setToday(playerAfk.getToday() + i);
            AFKRewardMapper.updatePlayer(pDO);
            AFKRewardMapper.AFK_MAP.remove(playerAfk.getPlayerName());
            return i;
        }
        return 0;
    }

    public static void updateOfflinePlayerEndTime() {
        AfkReward.getInstance().getLogger().info("§e正在更新意外离线玩家的挂机时间");
        AFKRewardMapper.updateOfflinePlayerEnd();
    }

}
